﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Setting
{
	/// <summary>
	/// Summary description for OrgSet.
	/// </summary>
	public class OrgSet : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.GroupBox GroupBox1;
		internal System.Windows.Forms.GroupBox GroupBox2;
		internal System.Windows.Forms.Label Label1;
		internal System.Windows.Forms.GroupBox GroupBox3;
		internal System.Windows.Forms.RadioButton rdoMinusDir;
		internal System.Windows.Forms.RadioButton rdoPulseDir;
		internal System.Windows.Forms.RadioButton rdoDirDisable;
		internal System.Windows.Forms.ComboBox cboZCount;
		internal System.Windows.Forms.CheckBox chkZUse;
		internal System.Windows.Forms.CheckBox chkOrg;
		internal System.Windows.Forms.Button btnReturn;
		internal System.Windows.Forms.CheckBox chkZ;
		internal System.Windows.Forms.CheckBox chkLimTurn;
		private System.Windows.Forms.Label lblOrg;
		private System.Windows.Forms.Label lblZ;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public OrgSet()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnReturn = new System.Windows.Forms.Button();
			this.GroupBox1 = new System.Windows.Forms.GroupBox();
			this.GroupBox2 = new System.Windows.Forms.GroupBox();
			this.rdoMinusDir = new System.Windows.Forms.RadioButton();
			this.rdoPulseDir = new System.Windows.Forms.RadioButton();
			this.rdoDirDisable = new System.Windows.Forms.RadioButton();
			this.cboZCount = new System.Windows.Forms.ComboBox();
			this.Label1 = new System.Windows.Forms.Label();
			this.chkZUse = new System.Windows.Forms.CheckBox();
			this.GroupBox3 = new System.Windows.Forms.GroupBox();
			this.lblZ = new System.Windows.Forms.Label();
			this.lblOrg = new System.Windows.Forms.Label();
			this.chkZ = new System.Windows.Forms.CheckBox();
			this.chkOrg = new System.Windows.Forms.CheckBox();
			this.chkLimTurn = new System.Windows.Forms.CheckBox();
			this.GroupBox1.SuspendLayout();
			this.GroupBox2.SuspendLayout();
			this.GroupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// btnReturn
			// 
			this.btnReturn.Location = new System.Drawing.Point(344, 240);
			this.btnReturn.Name = "btnReturn";
			this.btnReturn.Size = new System.Drawing.Size(88, 32);
			this.btnReturn.TabIndex = 1;
			this.btnReturn.Text = "Return";
			this.btnReturn.Click += new System.EventHandler(this.btnReturn_Click);
			// 
			// GroupBox1
			// 
			this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.GroupBox2,
																					this.cboZCount,
																					this.Label1,
																					this.chkZUse});
			this.GroupBox1.Location = new System.Drawing.Point(8, 16);
			this.GroupBox1.Name = "GroupBox1";
			this.GroupBox1.Size = new System.Drawing.Size(208, 256);
			this.GroupBox1.TabIndex = 5;
			this.GroupBox1.TabStop = false;
			this.GroupBox1.Text = "ORG Type";
			// 
			// GroupBox2
			// 
			this.GroupBox2.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.rdoMinusDir,
																					this.rdoPulseDir,
																					this.rdoDirDisable});
			this.GroupBox2.Location = new System.Drawing.Point(10, 96);
			this.GroupBox2.Name = "GroupBox2";
			this.GroupBox2.Size = new System.Drawing.Size(176, 136);
			this.GroupBox2.TabIndex = 4;
			this.GroupBox2.TabStop = false;
			this.GroupBox2.Text = "EndDirType :";
			// 
			// rdoMinusDir
			// 
			this.rdoMinusDir.Location = new System.Drawing.Point(16, 96);
			this.rdoMinusDir.Name = "rdoMinusDir";
			this.rdoMinusDir.Size = new System.Drawing.Size(128, 24);
			this.rdoMinusDir.TabIndex = 2;
			this.rdoMinusDir.Text = "Minus Dir";
			this.rdoMinusDir.CheckedChanged += new System.EventHandler(this.rdoMinusDir_CheckedChanged);
			// 
			// rdoPulseDir
			// 
			this.rdoPulseDir.Location = new System.Drawing.Point(16, 64);
			this.rdoPulseDir.Name = "rdoPulseDir";
			this.rdoPulseDir.Size = new System.Drawing.Size(128, 24);
			this.rdoPulseDir.TabIndex = 1;
			this.rdoPulseDir.Text = "Plus Dir";
			this.rdoPulseDir.CheckedChanged += new System.EventHandler(this.rdoPulseDir_CheckedChanged);
			// 
			// rdoDirDisable
			// 
			this.rdoDirDisable.Location = new System.Drawing.Point(16, 32);
			this.rdoDirDisable.Name = "rdoDirDisable";
			this.rdoDirDisable.Size = new System.Drawing.Size(128, 24);
			this.rdoDirDisable.TabIndex = 0;
			this.rdoDirDisable.Text = "Disable";
			this.rdoDirDisable.CheckedChanged += new System.EventHandler(this.rdoDirDisable_CheckedChanged);
			// 
			// cboZCount
			// 
			this.cboZCount.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboZCount.Location = new System.Drawing.Point(80, 54);
			this.cboZCount.Name = "cboZCount";
			this.cboZCount.Size = new System.Drawing.Size(104, 23);
			this.cboZCount.TabIndex = 3;
			this.cboZCount.SelectedIndexChanged += new System.EventHandler(this.cboZCount_SelectedIndexChanged);
			// 
			// Label1
			// 
			this.Label1.AutoSize = true;
			this.Label1.Location = new System.Drawing.Point(10, 56);
			this.Label1.Name = "Label1";
			this.Label1.Size = new System.Drawing.Size(58, 14);
			this.Label1.TabIndex = 1;
			this.Label1.Text = "Z Count : ";
			// 
			// chkZUse
			// 
			this.chkZUse.Location = new System.Drawing.Point(10, 24);
			this.chkZUse.Name = "chkZUse";
			this.chkZUse.TabIndex = 2;
			this.chkZUse.Text = "Z : USE";
			this.chkZUse.CheckedChanged += new System.EventHandler(this.chkZUse_CheckedChanged);
			// 
			// GroupBox3
			// 
			this.GroupBox3.Controls.AddRange(new System.Windows.Forms.Control[] {
																					this.lblZ,
																					this.lblOrg,
																					this.chkZ,
																					this.chkOrg});
			this.GroupBox3.Location = new System.Drawing.Point(232, 16);
			this.GroupBox3.Name = "GroupBox3";
			this.GroupBox3.Size = new System.Drawing.Size(200, 96);
			this.GroupBox3.TabIndex = 5;
			this.GroupBox3.TabStop = false;
			this.GroupBox3.Text = "ORG Logic";
			// 
			// lblZ
			// 
			this.lblZ.Location = new System.Drawing.Point(88, 64);
			this.lblZ.Name = "lblZ";
			this.lblZ.Size = new System.Drawing.Size(96, 20);
			this.lblZ.TabIndex = 11;
			this.lblZ.Text = "High Active";
			// 
			// lblOrg
			// 
			this.lblOrg.Location = new System.Drawing.Point(88, 32);
			this.lblOrg.Name = "lblOrg";
			this.lblOrg.Size = new System.Drawing.Size(96, 20);
			this.lblOrg.TabIndex = 10;
			this.lblOrg.Text = "High Active";
			// 
			// chkZ
			// 
			this.chkZ.Location = new System.Drawing.Point(12, 64);
			this.chkZ.Name = "chkZ";
			this.chkZ.Size = new System.Drawing.Size(56, 16);
			this.chkZ.TabIndex = 7;
			this.chkZ.Text = "Z";
			this.chkZ.Click += new System.EventHandler(this.chkZ_Click);
			// 
			// chkOrg
			// 
			this.chkOrg.Location = new System.Drawing.Point(12, 32);
			this.chkOrg.Name = "chkOrg";
			this.chkOrg.Size = new System.Drawing.Size(56, 16);
			this.chkOrg.TabIndex = 6;
			this.chkOrg.Text = "ORG";
			this.chkOrg.Click += new System.EventHandler(this.chkOrg_Click);
			// 
			// chkLimTurn
			// 
			this.chkLimTurn.Location = new System.Drawing.Point(245, 152);
			this.chkLimTurn.Name = "chkLimTurn";
			this.chkLimTurn.Size = new System.Drawing.Size(112, 24);
			this.chkLimTurn.TabIndex = 10;
			this.chkLimTurn.Text = "LimitTurn Use";
			this.chkLimTurn.CheckedChanged += new System.EventHandler(this.chkLimTurn_CheckedChanged);
			// 
			// OrgSet
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(440, 286);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnReturn,
																		  this.GroupBox1,
																		  this.GroupBox3,
																		  this.chkLimTurn});
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "OrgSet";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "ORG Setting";
			this.Load += new System.EventHandler(this.OrgSet_Load);
			this.GroupBox1.ResumeLayout(false);
			this.GroupBox2.ResumeLayout(false);
			this.GroupBox3.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnReturn_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void chkLimTurn_CheckedChanged(object sender, System.EventArgs e)
		{
			if ( chkLimTurn.Checked == true )
			{
				Setting.LimitTurn = 1;
			}
			else if ( chkLimTurn.Checked == false )
			{
				Setting.LimitTurn = 0;
			}
		}

		private void chkOrg_Click(object sender, System.EventArgs e)
		{
			int	bit=0x1;
			int	tmp = Setting.OrgLog;
			if( chkOrg.Checked == true )
			{
				lblOrg.Text = "Low Active";
				tmp = tmp & (~bit);
				Setting.OrgLog = (short)tmp;
			}
			else if( chkOrg.Checked == false )
			{
				lblOrg.Text = "High Active";
				tmp = tmp | bit;
				Setting.OrgLog = (short)tmp;
			}
		}

		private void chkZ_Click(object sender, System.EventArgs e)
		{
			int	bit=0x2;
			int	tmp = Setting.OrgLog;
			if( chkZ.Checked == true )
			{
				lblZ.Text = "Low Active";
				tmp = tmp & (~bit);
				Setting.OrgLog = (short)tmp;
			}
			else if( chkZ.Checked == false )
			{
				lblZ.Text = "High Active";
				tmp = tmp | bit;
				Setting.OrgLog = (short)tmp;
			}
		}

		private void chkZUse_CheckedChanged(object sender, System.EventArgs e)
		{
			if( chkZUse.Checked == true )
			{
				cboZCount.Enabled = true;
				Setting.OrgType = 1;
			}
			else if( chkZUse.Checked == false )
			{
				cboZCount.Enabled = false;
				Setting.OrgType = 0;
			}
		}

		private void cboZCount_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			Setting.ZCount = (short)(cboZCount.SelectedIndex + 1);
		}

		private void OrgSet_Load(object sender, System.EventArgs e)
		{
			// ORG Type Z:USE
			if( Setting.OrgType == 0 )
				chkZUse.Checked = false;
			else
				chkZUse.Checked = true;
			chkZUse_CheckedChanged(chkZUse, new System.EventArgs());
			// ZCount ComboBox
			string	wStr;
			cboZCount.Items.Clear();
			for( int i = 0; i < 16; i++ )
			{
				wStr = string.Format("{0:d}", i+1 );
				cboZCount.Items.Add(wStr);
			}
			// ComboBox Initialise
			if( Setting.ZCount > 0 )	cboZCount.SelectedIndex = Setting.ZCount-1;

			// EndDir OptionButton
			switch( Setting.EndDir )
			{
				case 0: // IDC_DIR_DISABLE
					rdoDirDisable.Checked = true;
					break;
				case 1: // IDC_PULSE_DIR
					rdoPulseDir.Checked = true;
					break;
				case 2: // IDC_MINUS_DIR
					rdoMinusDir.Checked = true;
					break;
			}

			// OrgLog CheckBox
			chkOrg.Checked = true;
			lblOrg.Text = "Low Active";
			if( (Setting.OrgLog & 0x1) != 0 )
			{
				chkOrg.Checked = false;
				lblOrg.Text = "High Active";
			}
			chkZ.Checked = true;
			lblZ.Text = "Low Active";
			if( (Setting.OrgLog & 0x2) != 0 )
			{
				chkZ.Checked = false;
				lblZ.Text = "High Active";
			}

			if ( Setting.LimitTurn == 1 )
			{
				chkLimTurn.Checked = true;
			}
			else if ( Setting.LimitTurn == 0 )
			{
				chkLimTurn.Checked = false;
			}
		}

		private void rdoDirDisable_CheckedChanged(object sender, System.EventArgs e)
		{
			Setting.EndDir = 0;
		}

		private void rdoPulseDir_CheckedChanged(object sender, System.EventArgs e)
		{
			Setting.EndDir = 1;
		}

		private void rdoMinusDir_CheckedChanged(object sender, System.EventArgs e)
		{
			Setting.EndDir = 2;
		}

	}
}
